package com.up.vms.interfaces.rest.config;

//import com.up.jiasai.interceptors.UserLoginInterceptor;

import com.up.jiasai.interceptors.UserLoginInterceptor;
import com.up.jiasai.spring.resolver.LoginArgumentResolver;
import com.up.vms.interfaces.rest.intercepters.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public HandlerInterceptor getTokenAuthenticationInterceptor(){
        return new TokenAuthenticationInterceptor();
    }

    @Bean
    public HandlerInterceptor getMerchantAuthenticationInterceptor(){
        return new MerchantAuthenticationInterceptor();
    }

    @Bean
    public HandlerInterceptor getMemberInfoManagerInterceptor() {
        return new MemberInfoInterceptor();
    }

    @Bean
    public HandlerInterceptor getUserLoginInterceptor() {
        return new UserLoginInterceptor();
    }

    @Resource
    private AccessRequiredInterceptor accessRequiredInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(getTokenAuthenticationInterceptor()).addPathPatterns("/**");
//        registry.addInterceptor(getMemberInfoManagerInterceptor()).addPathPatterns("/**");
//        registry.addInterceptor(accessRequiredInterceptor).addPathPatterns("/**");
//        registry.addInterceptor(getMerchantAuthenticationInterceptor())
//                .addPathPatterns(EmpAPIConstants.API_ROOT+"/merchants/**")
//                .addPathPatterns(EmpAPIConstants.API_ROOT+"/operate/**");

        //APP拦截器
        registry.addInterceptor(getUserLoginInterceptor()).addPathPatterns("/app/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        //客户端拦截ClientInfo对象，提供自动绑定功能
//        argumentResolvers.add(new RequestArgumentResolver());
        //登录拦截系统
        argumentResolvers.add(new LoginArgumentResolver());
    }

    @Bean
    public HttpServletRequestReplacedFilter getInputStreamFilter() {
        return new HttpServletRequestReplacedFilter();
    }

//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        //super.addViewControllers(registry);
//        registry.addViewController("/").setViewName("index");
//        registry.addViewController("/index.html").setViewName("index");
//    }

//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/").setViewName("forward:/index.html");
//    }
}
